package com.dzl.partition;

import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.partition.pojo.WelfarePartition;
import com.dzl.partition.pojo.query.WelfarePartitionQuery;
import com.dzl.partition.pojo.vo.WelfarePartitionVo;
import com.dzl.partition.service.WelfarePartitionService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 分区表(WelfarePartition)表控制层
 * @author caobucao
 * @date 2021-05-21 17:32:11
 */
@RestController
@RequestMapping("/welfarePartition")
public class WelfarePartitionController extends BaseController {

    @Autowired
    private WelfarePartitionService service;

    /**
     * 服务端获取所有未删除分区列表-不包含电影分区
     * @date 2021/6/4 10:00
     * @author caobucao
     * @param
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.partition.pojo.WelfarePartition>>
     */
    @GetMapping("getAllPartitionList")
    public JsonResult<List<WelfarePartition>> getAllPartitionList() {
        Example example = new Example(WelfarePartition.class);
        example.createCriteria().andEqualTo("status",1)
                .andNotEqualTo("id",1);
        return JsonResult.success(service.selectByExample(example));
    }

    /**
     * 服务端获取所有未删除分区列表-包含电影分区
     * @date 2021/6/4 10:00
     * @author caobucao
     * @param
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.partition.pojo.WelfarePartition>>
     */
    @GetMapping("getAllPartitionListContainMovie")
    public JsonResult<List<WelfarePartition>> getAllPartitionListContainMovie() {
        Example example = new Example(WelfarePartition.class);
        example.createCriteria().andEqualTo("status",1);
        return JsonResult.success(service.selectByExample(example));
    }

    /**
     * 分页查询分区列表
     * @date 2021/5/21 17:43
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.partition.pojo.vo.WelfarePartitionVo>
     */
    @GetMapping("queryPartitionVoList")
    public PageUtils<WelfarePartitionVo> queryPartitionVoList(WelfarePartitionQuery query) {
        PageInfo<WelfarePartitionVo> pageInfo = service.selectPartitionListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 分区下拉列表（不过滤无商品的分区，过滤全部分区）
     * @date 2021/9/6 0006 19:25
     * @author wangjian
     * @param query 查询条件
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.partition.pojo.vo.WelfarePartitionVo>>
     */
    @GetMapping("queryPartitionListData")
    public JsonResult<List<WelfarePartitionVo>> queryPartitionListData(WelfarePartitionQuery query) {
        return JsonResult.success(service.selectPartitionListData(query));
    }

    /**
     * 分区下拉列表（过滤无商品的分区）
     * @date 2021/8/24 0024 16:29
     * @author wangjian
     * @return
     */
    @GetMapping("queryPartitionList")
    public PageUtils<WelfarePartitionVo> queryPartitionList() {
        List<WelfarePartitionVo> list = service.selectPartitionList(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        PageInfo<WelfarePartitionVo> pageInfo = new PageInfo<>(list);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 保存分区 新增或修改
     * @date 2021/5/21 17:51
     * @author caobucao
     * @param welfarePartition 保存的分区对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("savePartition")
    public JsonResult<Integer> savePartition(@RequestBody WelfarePartition welfarePartition) {
        if(EmptyUtils.isEmpty(welfarePartition.getPartitionName())) {
            return JsonResult.paramErrWithMsg("请输入分区名称");
        }
        if(null == welfarePartition.getId()) {
            if(welfarePartition.getPartitionName().equals("全部分区")) {
                return JsonResult.paramErrWithMsg("不允许新增名称为“全部分区”的分区");
            }
            welfarePartition.setUpdater(getAdminInfo().getId());
            welfarePartition.setUpdaterName(getAdminInfo().getUserName());
            welfarePartition.setCreater(getAdminInfo().getId());
            welfarePartition.setCreaterName(getAdminInfo().getUserName());
            return JsonResult.success(service.insertSelective(welfarePartition));
        } else {
            if(welfarePartition.getId() == 0 ||welfarePartition.getPartitionName().equals("全部分区")) {
                return JsonResult.paramErrWithMsg("全部分区不允许修改");
            }
            welfarePartition.setUpdater(getAdminInfo().getId());
            welfarePartition.setUpdaterName(getAdminInfo().getUserName());
            welfarePartition.setUpdateTime(new Date());
            return JsonResult.success(service.updateByPrimaryKeySelective(welfarePartition));
        }
    }

    /**
     * 删除分区
     * @date 2021/5/21 17:51
     * @author caobucao
     * @param id 删除分区的id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("deletePartition")
    public JsonResult<Integer> deletePartition(Integer id) {
        if(null == id) {
            return JsonResult.paramErrWithMsg("请选择要删除的分区");
        }
        if(id == 0) {
            return JsonResult.paramErrWithMsg("全部分区不允许删除！");
        }
        //获取该分区下面是否有已关联商品
        Integer countGoods = service.selectCountGoodsById(id);
        if(countGoods > 0) {
            return JsonResult.paramErrWithMsg("该分区下已关联商品，无法删除！");
        }
        //查询分区是否已被卡类型关联
        Integer countCardType = service.selectCountCardTypeByPartitionId(id);
        if(countCardType > 0) {
            return JsonResult.paramErrWithMsg("该分区已被卡类型关联，无法删除！");
        }
        WelfarePartition welfarePartition = new WelfarePartition();
        welfarePartition.setId(id);
        welfarePartition.setUpdateTime(new Date());
        welfarePartition.setUpdater(getAdminInfo().getId());
        welfarePartition.setUpdaterName(getAdminInfo().getUserName());
        welfarePartition.setStatus(0);
        return JsonResult.success(service.updateByPrimaryKeySelective(welfarePartition));
    }

}
